package com.ztd.test;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,
        MyAdapter.IonSlidingViewClickListener {

    private static final String TAG = "MainActivity";
    @BindView(R.id.bt_a)
    Button btA;
    @BindView(R.id.bt_b)
    Button btB;
    @BindView(R.id.bt_c)
    Button btC;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    MyAdapter adapter;
    private static final int REFRESH_STATUS = 0;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    private int j = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        ViewTreeObserver viewTreeObserver = activityMain.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    activityMain.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    int width = activityMain.getWidth();
                    adapter.setWidth(width);
                }
            });
        }
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//这里用线性显示 类似于listview
        //下面的两种方式自己可以试试看下效果就知道了
        //mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));//这里用线性宫格显示 类似于grid view
        //mRecyclerView.setLayoutManager(new     StaggeredGridLayoutManager(2,OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
        adapter = new MyAdapter(this);
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.d(TAG, "onScrollStateChanged: " + newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.d(TAG, "onScrolled: " + dx + " == " + dy);
                if (adapter.menuIsOpen()) adapter.closeMenu();
            }
        });

        mRecyclerView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(MainActivity.this, "toast", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this);
        // 设置刷新时的颜色
        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);

        mSwipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(true);
            }
        });
        onRefresh();
    }

    @OnClick({R.id.bt_a, R.id.bt_b, R.id.bt_c})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_a:
                Singleton.instance().test("aaa");
                break;

            case R.id.bt_b:
                Singleton.instance().test("bbb");
                break;

            case R.id.bt_c:
                Singleton.instance().test("ccc");
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.bind(this).unbind();
    }

    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(MainActivity.this, "单击" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemLongClick(View view, int position) {
        Toast.makeText(this, "长安", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDeleteBtnCilck(View view, int position) {
        Toast.makeText(MainActivity.this, "删除" + position, Toast.LENGTH_SHORT).show();
        adapter.removeData(position);
    }

    @Override
//下拉刷新的监听
    public void onRefresh() {
        refreshHandler.sendEmptyMessageDelayed(REFRESH_STATUS, 2000);
    }

    private Handler refreshHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REFRESH_STATUS:
                    //下拉刷新执行的操作，刷新数据
                    mSwipeRefreshLayout.setRefreshing(false);
                    List<String> strings = new ArrayList<>();
                    for (int i = 0; i < 10; i++) {
                        j++;
                        strings.add("测试" + j);
                    }
                    adapter.updateData(strings);
                    adapter.isShow(true);
                    break;
            }
        }
    };
}
